package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
/**
 * 
 * @author axi
 *
 */
public class NfdailyNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://news.nfdaily.cn/content/[0-9]{4}-[0-9]{1,2}/[0-9]{1,2}/content_[0-9]+.htm",
				"http://wen.nfdaily.cn/content/[0-9]{4}-[0-9]{1,2}/[0-9]{1,2}/content_[0-9]+.htm",
				"http://bo.nfdaily.cn/timeline/content/[0-9]{4}-[0-9]{1,2}/[0-9]{1,2}/content_[0-9]+.htm",
				"http://wenzheng.nfdaily.cn/a/[0-9]{8}/[0-9]+.html",
				"http://wz.nfdaily.cn/Content/[0-9]+.htm"};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		/**
		 * 判断是否为详细页，不是详细页面直接返回空
		 */
		if (!isDetailPage(url)) {
		}
		String htmltxt=urlMeta.getHtml();
		String title="";
		String content="";
		Long date=null;
		Integer commentNum=0;
		Integer clickNum=0;
		NewsMeta news=new NewsMeta();
		Document doc=Jsoup.parse(htmltxt);

		if(url.indexOf("wenzheng.nfdaily.cn") != -1){
			Elements select=doc.select("div.nr");
			/**
			 * 解析标题
			 */
			Elements selectTitle=select.select("div.dbt.hei.fB");
			if(selectTitle.size() > 0){
				title=selectTitle.text();
			}
			/*
			 * 解析时间
			 */
			Elements selectDate=select.select("div.dbtex");
			if(selectDate.size() > 0){
				date=DateUtils.matchDate(selectDate.toString().replace("/", "-"));
			}
			/**
			 * 解析内容
			 */
			Elements selectContent=select.select("div.text.h14px.hei");
			if(selectContent.size() > 0){
				content=HtmlCleaner.getContentHtml(url,selectContent);
			}
		}else if(url.startsWith("http://wz.nfdaily.cn/")){
			/*
			 * 解析新闻标题
			 */
			title=doc.select("span.c-main.fB.f14px").text();
			/*
			 * 解析时间
			 */
			date=DateUtils.matchDate(doc.select("div.left.titleleft").text());
			/*
			 * 解析内容
			 */
			content=HtmlCleaner.getContentHtml(url,doc.select("div.f14px"));
		}else{
			
			Elements select=doc.select("div#article-content");
			if(select.size() > 0){
				/**
				 * 解析标题
				 */
				title=select.select("h1").text();
				/*
				 * 解析时间
				 */
				Elements selectDate=select.select("div#article-title > small");
				if(selectDate.size() > 0){
					date=DateUtils.matchDate(selectDate.toString());
				}
				/**
				 * 解析内容
				 */
				Elements selectContent=select.select("div#article-body");
				if(selectContent.size() > 0){
					content=HtmlCleaner.getContentHtml(url,selectContent);
				}
			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("h1").text();
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));
		}
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		if("".equals(title)){
		}
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);
		news.setUrl(url);
		news.setType(0);
		news.setCommentNum(commentNum);
		news.setClickNum(clickNum);
		
		String author=doc.select("div#article small").html();
		if(author!=null && author.indexOf("来源：")!=-1){
			author=StringUtils.substringAfter(author, "来源：").trim();
		}
		news.setAuthor(author);
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NfdailyNewsAnalyse n=new NfdailyNewsAnalyse();
//		String url="http://wenzheng.nfdaily.cn/a/20121107/1052830.html";
//		String url="http://news.nfdaily.cn/content/2012-11/21/content_58603949.htm";
//		String url="http://bo.nfdaily.cn/timeline/content/2012-11/22/content_58666856.htm";
		String url="http://epaper.nfdaily.cn/html/2014-07/11/content_7326443.htm";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		NewsMeta parserHtml=n.parserHtml(meta);
		System.out.println(parserHtml);
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
